home *** CD-ROM | disk | FTP | other *** search
- Path: sn.no!usenet
- From: jan-henrik.haukeland@fou.telenor.no (jan-henrik haukeland)
- Newsgroups: comp.lang.c
- Subject: Re: How to tell if a file exists in C
- Date: 05 Feb 1996 00:31:06 +0100
- Organization: LH*281263
- Message-ID: <m2pwbuvfb9.fsf@hawk.no>
- References: <4eqkj6$ipo@charm.magnus.acs.ohio-state.edu>
- NNTP-Posting-Host: nm6-ppp4.oslo.net
- In-reply-to: xiaoyi@bmecg.bme.ohio-state.edu's message of 1 Feb 1996 15:00:54
- GMT
- To: xiaoyi@mozart.bme.ohio-state.edu (Xiaoyi Wu)
- X-Newsreader: Gnus v5.0.12
-
- Xiaoyi Wu writes:
-
- : Hi, how do I find out if a file already exists
- : in UNIX C? On PCs I would do a findfirst/findnext,
- : is there an equivalent on Unix?
-
- Use the stat function, e.g.:
-
- #include <sys/stat.h>
- #include <unistd.h>
-
- /* Check if file exists */
- int fexist(char * filename)
- {
- struct stat buf;
-
- if (( stat (filename, &buf)) < 0)
- return (FALSE);
-
- if (! S_ISREG(buf.st_mode)) {
- return (NOT_A_REGULAR_FILE);
- }
-
- return(TRUE);
- }
-
-
-
- jhh
-